package com.czh.gzskzn.shenke3.Activity;

import android.app.Activity;
import android.os.Bundle;

import android.telecom.Call;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czh.gzskzn.shenke3.Activity.UtilServer.WanZhi;
import com.czh.gzskzn.shenke3.Activity.UtilT.Ting;
import com.czh.gzskzn.shenke3.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;


public class GongaoYe extends Activity {

//    int[] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};

    private TextView tou,shijain,zhengwen,fabu;
    private WebView webView;
    private String id;
    private LinearLayout Bake;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdianji);
//        this.setTitle("BaseAdapter for ListView");
        intiView();
        httpQinQiu();
        dianJi();



    }

    private void dianJi() {

        Bake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void httpQinQiu() {
        //        用okgo请求地址
        OkGo.get(WanZhi.TONZHIDZHI)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, okhttp3.Call call, Response response) {
                        List<Ting> list = getListPersonByGson(s);
                        for (int i = 1; i <= list.size(); i++) {
                            Log.e("ceshi", list.get(i).getContent());
                            String title = list.get(i).getTitle();
                            String date = list.get(i).getDate();
                            String publisher = list.get(i).getPublisher();
                            String content = list.get(i).getContent();
                            tou.setText(title);
                            shijain.setText(date);
//                        zhengwen.setText("\u3000\u3000" + content);
                            fabu.setText(publisher);

                            StringBuilder sb = new StringBuilder();
                            //拼接一段HTML代码
                            sb.append("<html>");
                            sb.append("<head>");
                            sb.append("</head>");
                            sb.append("<body>");
//                            将请求的html加入此处
                            sb.append(content);
                            sb.append("</body>");
                            sb.append("</html>");
                            //使用简单的loadData方法会导致乱码，可能是Android API的Bug
                            //show.loadData(sb.toString(), "text/html", "utf-8");
                            //加载、并显示HTML代码
                            webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
                        }
                    }

                });

    }

    private void intiView() {

        tou = (TextView) findViewById(R.id.tz_tv_you);
        shijain = (TextView) findViewById(R.id.tz_tv_shijian);
        fabu = (TextView) findViewById(R.id.tz_tv_tou2);
        webView = (WebView) findViewById(R.id.wd_wnga);
        Bake = (LinearLayout) findViewById(R.id.ll_tz_baike);


    }

    //通过Gson解析
    public  List<Ting> getListPersonByGson(String jsony) {
        List<Ting> list = new ArrayList<Ting>();
        Gson gson = new Gson();
        list = gson.fromJson(jsony, new TypeToken<List<Ting>>() {
        }.getType());
        return list;
    }

}
